package Lab12;

import java.io.*;

import java.util.*;


public class Ex12_3 {
    static void write() throws IOException {
        String filePath = "src/Lab12/rw.txt";

        FileWriter fw = new FileWriter(filePath);

        Random random = new Random();
        random.nextInt();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            int i1 = random.nextInt(99);
//            https://blog.csdn.net/weixin_34148508/article/details/93446582
            s.append(i1).append(" ");
        }
        fw.write(s.toString());
        fw.close();
    }

    static void read() throws IOException {
        String filePath = "src/Lab12/rw.txt";
        File file = new File(filePath);
        String encoding = "GBK";

        if (!file.isFile() || !file.exists()) {
            System.out.println("找不到指定的文件");
            return;
        }

        ArrayList<Integer> integers = new ArrayList<>();
        try (
                InputStreamReader inputStreamReader = new InputStreamReader(
                        new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            String[] s = bufferedReader.readLine().split(" ");
            for (String s1 : s) {
                integers.add(Integer.parseInt(s1));
            }


        } catch (EOFException e) {

        } finally {
//            写在try （）里面之后 貌似不用close了
            integers.sort(Comparator.naturalOrder());
//            https://www.runoob.com/java/java-arraylist-sort.html
            int cnt = 0;
            for (Integer i : integers) {
                cnt++;
                System.out.print(i + " ");
                if (cnt == 10) {
                    cnt = 0;
                    System.out.println();
                }
            }
        }

    }

    public static void main(String[] args) throws IOException {
//        write();
        read();
    }
}
